package com.company.oa.announcement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.oa.announcement.entity.AnnouncementRead;
import com.company.oa.announcement.mapper.AnnouncementReadMapper;
import com.company.oa.announcement.service.AnnouncementReadService;
import org.springframework.stereotype.Service;

@Service
public class AnnouncementReadServiceImpl extends ServiceImpl<AnnouncementReadMapper, AnnouncementRead> implements AnnouncementReadService {

    @Override
    public IPage<AnnouncementRead> getReadRecordPage(int page, int size, Long announcementId) {
        Page<AnnouncementRead> pageParam = new Page<>(page, size);
        return baseMapper.selectReadRecordPage(pageParam, announcementId);
    }

    @Override
    public boolean hasRead(Long announcementId, Long userId) {
        LambdaQueryWrapper<AnnouncementRead> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AnnouncementRead::getAnnouncementId, announcementId)
               .eq(AnnouncementRead::getUserId, userId);
        
        return baseMapper.selectCount(wrapper) > 0;
    }
} 